# SPDX-License-Identifier: BSD-2-Clause
%YAML 1.2
---
$id: http://devicetree.org/schemas/perf/riscv,pmu.yaml#
$schema: http://devicetree.org/meta-schemas/core.yaml#

title: RISC-V SBI PMU events

maintainers:
  - Atish Patra <atishp@rivosinc.com>

description: |
  The SBI PMU extension allows supervisor software to configure, start and
  stop any performance counter at anytime. Thus, a user can leverage all
  capabilities of performance analysis tools, such as perf, if the SBI PMU
  extension is enabled. The following constraints apply:

    The platform must provide information about PMU event to counter mappings
    either via device tree or another way, specific to the platform.
    Without the event to counter mappings, the SBI PMU extension cannot be used.

    Platforms should provide information about the PMU event selector values
    that should be encoded in the expected value of MHPMEVENTx while configuring
    MHPMCOUNTERx for that specific event. The can either be done via device tree
    or another way, specific to the platform.
    The exact value to be written to MHPMEVENTx is completely dependent on the
    platform.

    For information on the SBI specification see the section "Performance
    Monitoring Unit Extension" of:
      https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc

properties:
  compatible:
    const: riscv,pmu

  riscv,event-to-mhpmevent:
    $ref: /schemas/types.yaml#/definitions/uint32-matrix
    description:
      Represents an ONE-to-ONE mapping between a PMU event and the event
      selector value that the platform expects to be written to the MHPMEVENTx
      CSR for that event.
      The mapping is encoded in an matrix format where each element represents
      an event.
      This property shouldn't encode any raw hardware event.
    items:
      items:
        - description: event_idx, a 20-bit wide encoding of the event type and
            code. Refer to the SBI specification for a complete description of
            the event types and codes.
        - description: upper 32 bits of the event selector value for MHPMEVENTx
        - description: lower 32 bits of the event selector value for MHPMEVENTx

  riscv,event-to-mhpmcounters:
    $ref: /schemas/types.yaml#/definitions/uint32-matrix
    description:
      Represents a MANY-to-MANY mapping between a range of events and all the
      MHPMCOUNTERx in a bitmap format that can be used to monitor these range
      of events. The information is encoded in an matrix format where each
      element represents a certain range of events and corresponding counters.
      This property shouldn't encode any raw event.
    items:
      items:
        - description: first event_idx of the range of events
        - description: last event_idx of the range of events
        - description: bitmap of MHPMCOUNTERx for this event

  riscv,raw-event-to-mhpmcounters:
    $ref: /schemas/types.yaml#/definitions/uint32-matrix
    description:
      Represents an ONE-to-MANY or MANY-to-MANY mapping between the rawevent(s)
      and all the MHPMCOUNTERx in a bitmap format that can be used to monitor
      that raw event.
      The encoding of the raw events are platform specific. The information is
      encoded in a matrix format where each element represents the specific raw
      event(s).
      If a platform directly encodes each raw PMU event as a unique ID, the
      value of variant must be 0xffffffff_ffffffff.
    items:
      items:
        - description:
            upper 32 invariant bits for the range of events
        - description:
            lower 32 invariant bits for the range of events
        - description:
            upper 32 bits of the variant bit mask for the range of events
        - description:
            lower 32 bits of the variant bit mask for the range of events
        - description:
            bitmap of all MHPMCOUNTERx that can monitor the range of events

dependencies:
  "riscv,event-to-mhpmevent": [ "riscv,event-to-mhpmcounters" ]
  "riscv,event-to-mhpmcounters": [ "riscv,event-to-mhpmevent" ]

required:
  - compatible

additionalProperties: false

examples:
  - |
    pmu {
        compatible = "riscv,pmu";
        riscv,event-to-mhpmevent = <0x0000B 0x0000 0x0001>;
        riscv,event-to-mhpmcounters = <0x00001 0x00001 0x00000001>,
                                      <0x00002 0x00002 0x00000004>,
                                      <0x00003 0x0000A 0x00000ff8>,
                                      <0x10000 0x10033 0x000ff000>;
        riscv,raw-event-to-mhpmcounters =
            /* For event ID 0x0002 */
            <0x0000 0x0002 0xffffffff 0xffffffff 0x00000f8>,
            /* For event ID 0-4 */
            <0x0 0x0 0xffffffff 0xfffffff0 0x00000ff0>,
            /* For event ID 0xffffffff0000000f - 0xffffffff000000ff */
            <0xffffffff 0x0 0xffffffff 0xffffff0f 0x00000ff0>;
    };

  - |
    /*
     * For HiFive Unmatched board the encodings can be found here
     * https://sifive.cdn.prismic.io/sifive/1a82e600-1f93-4f41-b2d8-86ed8b16acba_fu740-c000-manual-v1p6.pdf
     *
     * This example also binds standard SBI PMU hardware IDs to U74 PMU event
     * codes, U74 uses a bitfield for events encoding, so several U74 events
     * can be bound to a single perf ID.
     * See SBI PMU hardware IDs in arch/riscv/include/asm/sbi.h
     */
    pmu {
          compatible = "riscv,pmu";
          riscv,event-to-mhpmevent =
              /* SBI_PMU_HW_CACHE_REFERENCES -> Instruction or Data cache/ITIM busy */
              <0x00003 0x00000000 0x1801>,
              /* SBI_PMU_HW_CACHE_MISSES -> Instruction or Data cache miss or MMIO access */
              <0x00004 0x00000000 0x0302>,
              /* SBI_PMU_HW_BRANCH_INSTRUCTIONS -> Conditional branch retired */
              <0x00005 0x00000000 0x4000>,
              /* SBI_PMU_HW_BRANCH_MISSES -> Branch or jump misprediction */
              <0x00006 0x00000000 0x6001>,
              /* L1D_READ_MISS -> Data cache miss or MMIO access */
              <0x10001 0x00000000 0x0202>,
              /* L1D_WRITE_ACCESS -> Data cache write-back */
              <0x10002 0x00000000 0x0402>,
              /* L1I_READ_ACCESS -> Instruction cache miss */
              <0x10009 0x00000000 0x0102>,
              /* LL_READ_MISS -> UTLB miss */
              <0x10011 0x00000000 0x2002>,
              /* DTLB_READ_MISS -> Data TLB miss */
              <0x10019 0x00000000 0x1002>,
              /* ITLB_READ_MISS-> Instruction TLB miss */
              <0x10021 0x00000000 0x0802>;
          riscv,event-to-mhpmcounters = <0x00003 0x00006 0x18>,
                                        <0x10001 0x10002 0x18>,
                                        <0x10009 0x10009 0x18>,
                                        <0x10011 0x10011 0x18>,
                                        <0x10019 0x10019 0x18>,
                                        <0x10021 0x10021 0x18>;
          riscv,raw-event-to-mhpmcounters = <0x0 0x0 0xffffffff 0xfc0000ff 0x18>,
                                            <0x0 0x1 0xffffffff 0xfff800ff 0x18>,
                                            <0x0 0x2 0xffffffff 0xffffe0ff 0x18>;
    };
